@echo off

setlocal enabledelayedexpansion

REM conversion utility program, courtesy of ConvertZ
set convertz_exe=D:\zerox\bin\ConvertZ\ConvertZ.exe

REM output encoding
set out_enc=ube
REM inplace editing or not
set inplace=0
REM if NOT inplace, then output to another .txt
REM file with !suffix! as suffix of the filename
set suffix=U

if "%~1"=="" echo USAGE: %~nx0 FILE&&exit /b -1

:get_opts
echo %~1 | findstr /R "^-" >nul
if errorlevel 1 goto main_sub

if "%~1"=="-utf8" set out_enc=utf8
if "%~1"=="-ube" set out_enc=ube
if "%~1"=="-ule" set out_enc=ule
if "%~1"=="-i" set inplace=1

shift
goto get_opts

:main_sub
echo Suffix: !suffix!
echo Out encoding: !out_enc!
echo Inplace: !inplace!

call :convert_sub "%~1"
goto end

REM ==============================================================

:convert_sub
if "%~x1"==".txt" (
    echo .TXT found
    echo.
    echo Processing [%~1]
    if !inplace!==1 (
        %convertz_exe% /o:!out_enc! /f:d "%~1"
    ) else (
        %convertz_exe% /o:!out_enc! /f:d "%~1" "%~dnp1.%suffix%.txt"
    )
    echo ===============================================================
)

if "%~x1"==".tmp" (
    echo .TMP found, assume as file list
    for /F "delims=*" %%j in (%~1) do (
        echo.
        echo Processing [%%~j]
        if !inplace!==1 (
            %convertz_exe% /o:!out_enc! /f:d "%%~j"
        ) else (
            %convertz_exe% /o:!out_enc! /f:d "%%~j" "%%~dpnj.%suffix%.txt"
        )
        echo ===============================================================
    )
)
goto :eof

REM ==============================================================

:end
echo -= DoNE =-

endlocal

pause
